package Collection;

import com.sun.org.apache.xpath.internal.functions.FuncFalse;

import java.util.HashSet;
import java.util.Objects;

//Object类，equals方法
class Student {
   private String id;//学号
   private String name;//姓名

    public Student(String id, String name) {
        this.id = id;
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return  "id=" + id + ", name=" + name ;
    }
    //Student对象的相等----学号和姓名相同

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        //当前对象this，参数传递的对象o
        //返回值，相等-true；不相等-false
        return  this.id.equals(student.id)&&this.name.equals(student.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name);
    }
}


public class TestHashSet {
    public static void main(String[] args) {
        //集合中存入对象
        HashSet studentSet =new HashSet();
        Student student1=new Student("1001","tom");
        studentSet.add(student1);
        studentSet.add(new Student("1002","jerry"));
        //重复元素
        studentSet.add(new Student("1002","jerry"));
        //输出集合元素
        System.out.println(studentSet);
    }
}
